package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.config.common.BaseContext;
import com.itheima.reggie.config.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author BaiXin
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public List<ShoppingCart> list(Long userId) {
        //条件构造


        //查询
        Wrapper qw = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = this.list(qw);

        //返回
        return list;
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart 购物车项
     * @return 添加成功的购物车项
     */
    @Override
    public ShoppingCart saveAndReturn(ShoppingCart shoppingCart) {

        // 获取当前登录用户的id
        Long userId = BaseContext.getThreadLocal();

        // 设置进购物车项对象
        shoppingCart.setUserId(userId);

        // 查询购物车项是否存在
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        Wrapper<ShoppingCart> qw
                = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, userId)
                // 可选设置条件：1、dishId有值，就使用该条件
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                // 可选设置条件：2、setmealId有值，就使用该条件
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);

        ShoppingCart scInDb = this.getOne(qw);
        // 存在，数量+1 ，更新
        if (scInDb != null) {
            scInDb.setNumber(scInDb.getNumber() + 1);
            shoppingCart = scInDb;
        } else {
            shoppingCart.setNumber(1);
        }
        this.saveOrUpdate(shoppingCart);
        return shoppingCart;
    }

    /**
     * 减少购物车
     *
     * @param shoppingCart 购物车项
     * @return 添加成功的购物车项
     */
    @Override
    public ShoppingCart deleteAndReturn(ShoppingCart shoppingCart) {

        // 查询购物车项是否存在
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        Wrapper<ShoppingCart> qw
                = Wrappers.lambdaQuery(ShoppingCart.class)
                // 可选设置条件：1、dishId有值，就使用该条件
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                // 可选设置条件：2、setmealId有值，就使用该条件
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);

        ShoppingCart scInDb = this.getOne(qw);
        if (scInDb.getNumber() > 1) {
            scInDb.setNumber(scInDb.getNumber() - 1);
            this.updateById(scInDb);
        }
        else if (scInDb.getNumber() == 1) {
            this.removeById(scInDb.getId());
            scInDb.setNumber(null);
        }
        return scInDb;
    }


}